package com.example.demo.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {
	@Autowired
	UserService userService;
	@RequestMapping("/getUserList")
	public String getUserList(Model model) {
		List<User> getlist=userService.getUserList();
		model.addAttribute("getlist", getlist);
		return "/user/ht_yonghu";
	}
	@RequestMapping("searchUserList")
	public String searchUserList(Model model,String user_name,String  vip) {
		List<User> getlist=userService.searchUserList(user_name, vip);
		model.addAttribute("getlist",getlist);
		return "/user/ht_yonghu";
	}
	
	@RequestMapping("/deleteUser")
	public String deleteUser(User user,Model model) {
		boolean f=userService.deleteUser(user);
		if(f) {
			return "forward:/user/getUserList";
		}
		else {
			model.addAttribute("result","no");
			model.addAttribute("msg","用户还存在没有删除的歌单");
			return "/user/result";	
		}
	}
	
	@RequestMapping("/addUser")
	public String addUser(User user,Model model) {
		int i=userService.addUser(user);
		if(i==0) {
			model.addAttribute("result","yes");
			model.addAttribute("msg","恭喜您，注册成功！");
		}else if(i==1) {
			model.addAttribute("result","no");
			model.addAttribute("msg","恭喜您，该用户也注册，请更换！");
		}else if(i==2) {
			model.addAttribute("result","no");
			model.addAttribute("msg","对不起，注册失败！");
		}
		return"forward:/user/getUserList";
	}
	@RequestMapping("/login")
	public String login(User user,HttpSession session,Model model) {
		User chUser=userService.existsUser(user);
		if(chUser!=null) {
			if(chUser.getUser_realname().equals("admin")) {
				return "forward:/user/getUserList";
			}
			session.setAttribute("login_user", chUser);
			
			return "forward:/sheet/shouye";
		}
		else{
			model.addAttribute("reg","用户或密码错误");
			return "index";
		}
		
	}

	//展示柱状图
    @RequestMapping( "/show")
    @ResponseBody
    public List<User> show() {
        List<User> users=userService.userList();
        return users;

    }

	@RequestMapping("/getList")
	public String getList(Model model,int user_id) {
		User user=userService.getList(user_id);
		model.addAttribute("user",user);
		return "xiugaigrxx";
	}
	@RequestMapping("/updateUser")
	public String updateUser(Model model,User user) {
		boolean f=userService.updateUser(user);
		if(f) {
			return "forward:/user/getUserList";
		}
		else {
			model.addAttribute("result","no");
			model.addAttribute("msg","对不起，修改失败！");
			return "/user/result";	
		}
	}

}
